// utils/storage.js
const STORAGE_KEYS = {
  TASKS: 'focus_tasks',
  SETTING: 'focus_setting'
}

// 获取所有任务
export const getTasks = () => {
  return new Promise((resolve) => {
    uni.getStorage({
      key: STORAGE_KEYS.TASKS,
      success: (res) => {
        resolve(res.data || [])
      },
      fail: () => {
        resolve([])
      }
    })
  })
}

// 保存所有任务
export const saveTasks = (tasks) => {
  return new Promise((resolve) => {
    uni.setStorage({
      key: STORAGE_KEYS.TASKS,
      data: tasks,
      success: () => {
        resolve()
      }
    })
  })
}

// 获取单个任务
export const getTask = (id) => {
  return new Promise(async (resolve) => {
    const tasks = await getTasks()
    const task = tasks.find(task => task.id === id)
    resolve(task || null)
  })
}

// 添加新任务
export const addTask = (task) => {
  return new Promise(async (resolve) => {
    const tasks = await getTasks()
    const newTask = {
      id: Date.now().toString(),
      createdAt: new Date().toISOString(),
      ...task,
      completed: false
    }
    tasks.unshift(newTask)
    await saveTasks(tasks)
    resolve(newTask)
  })
}

// 更新任务
export const updateTask = (task) => {
  return new Promise(async (resolve) => {
    const tasks = await getTasks()
    const taskIndex = tasks.findIndex(t => t.id === task.id)
    if (taskIndex !== -1) {
      tasks[taskIndex] = {
        ...tasks[taskIndex],
        ...task
      }
      await saveTasks(tasks)
      resolve(tasks[taskIndex])
    } else {
      resolve(null)
    }
  })
}

// 删除任务
export const deleteTask = (id) => {
  return new Promise(async (resolve) => {
    const tasks = await getTasks()
    const newTasks = tasks.filter(task => task.id !== id)
    await saveTasks(newTasks)
    resolve()
  })
}

// 标记任务为已完成
export const completeTask = (id) => {
  return new Promise(async (resolve) => {
    const tasks = await getTasks()
    const taskIndex = tasks.findIndex(t => t.id === id)
    if (taskIndex !== -1) {
      tasks[taskIndex].completed = true
      tasks[taskIndex].completedAt = new Date().toISOString()
      await saveTasks(tasks)
      resolve(tasks[taskIndex])
    } else {
      resolve(null)
    }
  })
}

// 获取设置
export const getSetting = () => {
  return new Promise((resolve) => {
    uni.getStorage({
      key: STORAGE_KEYS.SETTING,
      success: (res) => {
        resolve(res.data || {
          soundReminder: true,
          vibrateReminder: true,
          notificationReminder: true,
          ringtone: 'default',
          darkMode: uni.getSystemInfoSync().theme === 'dark'
        })
      },
      fail: () => {
        resolve({
          soundReminder: true,
          vibrateReminder: true,
          notificationReminder: true,
          ringtone: 'default',
          darkMode: uni.getSystemInfoSync().theme === 'dark'
        })
      }
    })
  })
}

// 保存设置
export const saveSetting = (setting) => {
  return new Promise((resolve) => {
    uni.setStorage({
      key: STORAGE_KEYS.SETTING,
      data: setting,
      success: () => {
        resolve()
      }
    })
  })
}  